package com.transaction.manage.service;

import com.transaction.common.base.PageInfo;
import com.transaction.common.util.PageUtil;
import com.transaction.manage.localMapper.ChannelInfoMapper;
import com.transaction.manage.localMapper.MarketDateMapper;
import com.transaction.manage.model.dto.ChannelInfo;
import com.transaction.manage.model.dto.MarketDate;
import com.transaction.manage.model.vo.ComboboxVo;
import com.transaction.manage.model.vo.MarketDateVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BaseParamsService {

    @Autowired
    private MarketDateMapper marketDateMapper;

    @Autowired
    private ChannelInfoMapper channelInfoMapper;

    @Autowired
    private ComboboxService comboboxService;

    @Autowired
    private TaService taService;

    /**
     * 保存市场交易日
     * @param marketDateVo
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveMarketDates(MarketDateVo marketDateVo) {
        String marketCode = marketDateVo.getMarketCode();
        String year = marketDateVo.getYear();
        List<MarketDate> marketDates = marketDateVo.getMarketDates();
        if (StringUtils.isEmpty(marketCode) || StringUtils.isEmpty(year)) {
            throw new RuntimeException("市场编码/年份不允许为空");
        }

        //1.删除该市场已存在保存年份的市场交易日
        marketDateMapper.deleteMarketDate(marketCode,year);

        if(CollectionUtils.isNotEmpty(marketDates)) {
            //2.保存该市场选择的市场交易日
            marketDateMapper.insertMarketDates(marketDateVo);
        }
    }

    /**
     * 查询市场交易日数据
     * @param marketDate 市场交易日对象
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<MarketDate> selectMarketDates(MarketDate marketDate) {
        return marketDateMapper.selectMarketDates(marketDate);
    }

    /**
     * 查询渠道信息
     *
     * @param channelInfo 渠道信息
     * @param pageInfo    分页信息
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public List<ChannelInfo> selectChannelInfoList(ChannelInfo channelInfo, PageInfo pageInfo) {
        PageUtil.StartPage(pageInfo);
        return channelInfoMapper.selectChannelInfoList(channelInfo);
    }

    /**
     * 保存渠道信息
     *
     * @param channelInfo 渠道信息
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveChannelInfo(ChannelInfo channelInfo) {
        Integer id = channelInfo.getId();
        if (id != null) {//直接更新
            //更新已存在的状态为为修改
            exchangeChannelArrFieldToStr(channelInfo);
            channelInfoMapper.updateChannelInfo(channelInfo);
            return;
        }

        //1.校验渠道编码是否已存在，更新
        ChannelInfo oldChannel = channelInfoMapper.selectOne(new ChannelInfo(channelInfo.getChannelCode()));
        if (oldChannel != null) {
            //更新已存在的状态为为新增
            exchangeChannelArrFieldToStr(channelInfo);
            channelInfoMapper.updateChannelInfo(channelInfo);
            return;
        }
        //2.数组转字符串存入
        exchangeChannelArrFieldToStr(channelInfo);
        channelInfoMapper.insertChannelInfo(channelInfo);
    }

    private void exchangeChannelArrFieldToStr(ChannelInfo channelInfo) {
        channelInfo.setIndiOpenAccountType(String.join(",",channelInfo.getIndiOpenAccountTypeArr()));
        channelInfo.setInstOpenAccountType(String.join(",",channelInfo.getInstOpenAccountTypeArr()));
        channelInfo.setDealFileType(String.join(",",channelInfo.getDealFileTypeArr()));
        channelInfo.setBeforeCfmFundType(String.join(",",channelInfo.getBeforeCfmFundTypeArr()));
        channelInfo.setVolDetailFundType(String.join(",",channelInfo.getVolDetailFundTypeArr()));
        channelInfo.setSecondForceAddFundType(String.join(",",channelInfo.getSecondForceAddFundTypeArr()));
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteChannelInfo(String channelCode) {
        if (StringUtils.isEmpty(channelCode)) {
            throw new RuntimeException("渠道编码不允许为空");
        }
        channelInfoMapper.deleteChannel(channelCode);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void checkChannelInfo(String channelCode) {
        if (StringUtils.isEmpty(channelCode)) {
            throw new RuntimeException("渠道编码不允许为空");
        }
        channelInfoMapper.checkChannel(channelCode);
    }

    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public Map<String, Object> selectChannelInfoModalAllCombobox() {
        Map<String, Object> map = new HashedMap<>();
        //TA渠道编码
        List<ComboboxVo> channelCodes = taService.getTaChannelInfoCombobox();
        //产品类型
        List<ComboboxVo> fundTypes = comboboxService.selectAllChildrenByDictKey("fundType");
        //个人开户类型
        List<ComboboxVo> indiOpenAccountTypes = comboboxService.selectAllChildrenByDictKey("indiCertificateType");
        //机构开户类型
        List<ComboboxVo> instOpenAccountTypes = comboboxService.selectAllChildrenByDictKey("instCertificateType");

        map.put("fundTypes", fundTypes);
        map.put("indiOpenAccountTypes", indiOpenAccountTypes);
        map.put("instOpenAccountTypes", instOpenAccountTypes);
        map.put("channelCodes", channelCodes);

        return map;
    }
}
